package com.xhsj.user.hr.system.web;

import com.xhsj.user.hr.system.entity.TbGlobalMenu;
import com.xhsj.user.hr.system.mapper.TbGlobalOrganizationMapper;
import com.xhsj.user.hr.system.query.GetListByParentIdQuery;
import com.xhsj.user.hr.system.vo.OrganizationTreeVo;
import com.xhsj.user.utils.Message;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;


import com.xhsj.user.hr.system.service.TbGlobalOrganizationService;
import com.xhsj.user.hr.system.entity.TbGlobalOrganization;


/**
 * <p>
 * 组织结构表 前端控制器
 * </p>
 *
 * @author 苏小林
 * @since Sep 1, 2020 1:50:18 PM
 */
@Slf4j
@Api(tags = "组织结构表")
@RestController
@RequestMapping("/system/tbGlobalOrganization")
public class TbGlobalOrganizationController {

	@Autowired
    public TbGlobalOrganizationService service;

	@Autowired
    public TbGlobalOrganizationMapper tbGlobalOrganizationMapper;


	/**
	 * 保存/修改
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
	@ApiOperation(value = "组织结构表,保存/修改", notes = "组织结构表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:global:organization:edit')")
	public Message save(@RequestBody TbGlobalOrganization data){
    	try {
			Integer type = data.getType();
			if (!data.getOldName().equals(data.getName())) {
				int count = tbGlobalOrganizationMapper.getByNameIsRepeat(type,data.getName());
				if (count > 0) {
					return Message.error(1,data.getName()+":已经存在,请重新输入");
				}
			}

    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 查询组织结构部门树
	 * @author 苏小林
	 * @date 2020/9/2 14:37
	 */
	@ApiOperation(value = "查询组织结构部门树", notes = "查询组织结构部门树")
	@RequestMapping(value = "/findOrganizationTree", method = RequestMethod.GET)
	public Message findOrganizationTree() {
		try {
			List<OrganizationTreeVo> list = service.findOrganizationTree();
			return Message.success("成功",list);
		} catch (Exception e) {
			log.error("失败:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 通过id删除数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
	@ApiOperation(value = "组织结构表删除", notes = "通过id删除组织结构表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:global:organization:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.delteById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}
	}


	/**
	 * 批量删除
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:global:organization:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

	/**
	 * @Description:  部门列表
	 * @Param:  com.xhsj.user.hr.process.dto.ProcessDefinition.AddBusinessDto
	 * @Author: liuqinghua
	 * @Date: 2020/8/31
	 */
	@ApiOperation(value = "部门列表", notes = "部门列表")
	@RequestMapping(value="/organizationList",method = RequestMethod.GET)
	public Message organizationList() {
		return Message.success("成功",service.organizationList());
	}

	/**
	 * 查询所有职位列表
	 * @author 苏小林
	 * @date 2020/9/2 16:53
	 */
	@ApiOperation(value = "查询所有职位列表", notes = "查询所有职位列表")
	@RequestMapping(value="/findPositionList",method = RequestMethod.GET)
	public Message findPositionList() {
		return Message.success("成功",service.findPositionList());
	}

    /**
    * 分页查询数据
    * @author 苏小林
    * @date Sep 1, 2020 1:50:18 PM
    */
	@ApiOperation(value = "分页查询组织结构表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:global:organization:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbGlobalOrganization> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}


	/**
	 * 查询parentId下所有列表数据
	 * @author 苏小林
	 * @date 2020/9/2 15:37
	 */
    @ApiOperation(value = "查询parentId下所有列表数据", notes = "查询parentId下所有列表数据")
    @RequestMapping(value="/getListByParentId",method = RequestMethod.POST)
	public Message getListByParentId(@RequestBody GetListByParentIdQuery getListByParentIdQuery) {
        try {
			IPage<TbGlobalOrganization> page = service.getListByParentId(getListByParentIdQuery);
            return Message.success("成功",page.getRecords(),page.getTotal(),page.getPages());
        } catch (Exception e) {
            log.error("异常:{}",e);
            return Message.error(1,"失败");
        }
    }

    /**
    * 查询所有数据
    * @author 苏小林
    * @date Sep 1, 2020 1:50:18 PM
    */
	@ApiOperation(value = "查询组织结构表所有数据list", notes = "查询组织结构表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:global:organization:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author 苏小林
    * @date Sep 1, 2020 1:50:18 PM
    */
	@ApiOperation(value = "通过id获取组织结构表实体类", notes = "通过id获取组织结构表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:organization:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author 苏小林
    * @date Sep 1, 2020 1:50:18 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:organization:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

}

